package edu.aynu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.aynu.entity.User;
import edu.aynu.service.UserService;
import edu.aynu.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 管理员操作
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private UserService userService;

    /**
     *   根据ID修改，需要传ID
     *  只能修改  姓名和邮箱和密码     注册时间和账号不能修改
     * @param user
     * @return
     */
    @PutMapping("/user")
    public Result updateUserById(@RequestBody User user){
        int i = userService.updateUserById(user);
        if (i > 0){
            return Result.success().add("tips","修改成功");
        }else {
            return Result.fail().add("tips","修改失败");
        }
    }

    /**
     * 显示所有用户
     * @return
     */
    @GetMapping("/users/limit")
    public Result showUser(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,5);
        List<User> users = userService.queryAll();
        PageInfo<User> pageInfo = new PageInfo<User>(users);

        if (users != null){
            return Result.success().add("users",pageInfo);
        } else {
            return Result.fail().add("tips","当前没有用户");
        }
    }

    /**
     * 删除多个用户
     * @RequestBody
     * @param ids
     * @return
     */
    @DeleteMapping("/user")
    public Result deletes(Integer[] ids){
        int i = userService.deleteUserByIds(ids);
        if (i > 0){
            return Result.success().add("tips","批量删除完成");
        }else {
            return Result.fail().add("tips","批量删除出现问题");
        }
    }
}
